//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using EntLibContrib.Query.Configuration;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query
{
	/// <summary>
	/// Represents a view to navigate the <see cref="QuerySettings"/> configuration data.
	/// </summary>
	public class QueryConfigurationView
	{
		#region Fields
		private IConfigurationSource configurationSource;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the <see cref="QuerySettings"/> configuration data.
		/// </summary>
		/// <returns>
		/// The <see cref="QuerySettings"/> configuration data.
		/// </returns>
		[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
		public QuerySettings QuerySettings
		{
			get 
			{
				QuerySettings settings = (QuerySettings)configurationSource.GetSection(QuerySettings.SectionName);
				if (settings == null)
					throw new ConfigurationErrorsException(InternalResources.MissingQuerySection);

				return settings;
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="QueryConfigurationView"/> with an <see cref="IConfigurationSource"/>.
		/// </summary>
		/// <param name="configurationSource">An <see cref="IConfigurationSource"/> object.</param>
		public QueryConfigurationView (IConfigurationSource configurationSource)
		{
			if (configurationSource == null)
				throw new ArgumentNullException("configurationSource");

			this.configurationSource = configurationSource;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Gets the <see cref="QueryDataBase"/> from configuration for the named <see cref="IQuery"/>
		/// </summary>
		/// <param name="queryName">The name of the <see cref="IQuery"/>.</param>
		/// <returns>
		/// A <see cref="QueryDataBase"/> object.
		/// </returns>
		public QueryDataBase GetQueryData(string queryName)
		{
			if (string.IsNullOrEmpty(queryName))
				throw new ConfigurationErrorsException(InternalResources.ExceptionQueryNameNullOrEmpty);

			QuerySettings configSettings = this.QuerySettings;
			if (!configSettings.Queries.Contains(queryName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionProviderNotDefined, queryName));

			return configSettings.Queries.Get(queryName);
		}

		/// <summary>
		/// Gets the <see cref="ParameterSetData"/> from configuration for the named <see cref="IParameterSet"/>
		/// </summary>
		/// <param name="parameterSetName">The name of the <see cref="IParameterSet"/>.</param>
		/// <returns>
		/// A <see cref="ParameterSetData"/> object.
		/// </returns>
		public ParameterSetData GetParameterSetData(string parameterSetName)
		{
			if (string.IsNullOrEmpty(parameterSetName))
				throw new ConfigurationErrorsException(InternalResources.ExceptionParameterSetNameNullOrEmpty);

			QuerySettings configSettings = this.QuerySettings;
			if (!configSettings.ParameterSets.Contains(parameterSetName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionProviderNotDefined, parameterSetName));

			return configSettings.ParameterSets.Get(parameterSetName);
		}

		/// <summary>
		/// Gets the <see cref="CommandData"/> from configuration for the named <see cref="ICommand"/>
		/// </summary>
		/// <param name="customCommandName">Name of the custom command.</param>
		/// <returns>
		/// A <see cref="CustomCommandData"/> object.
		/// </returns>
		public CommandData GetCommandData(string customCommandName)
		{
			if (string.IsNullOrEmpty(customCommandName))
				throw new ConfigurationErrorsException(InternalResources.ExceptionCustomCommandNameNullOrEmpty);

			QuerySettings configSettings = this.QuerySettings;
			if (!configSettings.Commands.Contains(customCommandName))
				throw new ConfigurationErrorsException(string.Format(InternalResources.Culture, InternalResources.ExceptionProviderNotDefined, customCommandName));

			return configSettings.Commands.Get(customCommandName);
		}
		#endregion
	}
}